package algorithm7.leetcode.剑指Offer;

import org.junit.jupiter.api.Test;

public class JZ05 {

    public String replaceSpace(String s) {
//        return handle1(s);
        return handle2(s);
    }

    private String handle2(String s) {
        int n = s.length();
        char[] chars = new char[3 * n];
        int size = 0;
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (' ' == ch) {
                chars[size++] = '%';
                chars[size++] = '2';
                chars[size++] = '0';
                continue;
            }
            chars[size++] = ch;
        }
        return String.valueOf(chars, 0, size);
    }

    private String handle1(String s) {
        return s.replace(" ", "%20");
    }

    /*

剑指 Offer 05. 替换空格
请实现一个函数，把字符串 s 中的每个空格替换成"%20"。

示例 1：
输入：s = "We are happy."
输出："We%20are%20happy."

     */

    @Test
    void t() {
        String s = "We are happy.";
        String res = replaceSpace(s);
        System.out.println("res = " + res);
    }

}
